package br.com.testes;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

public class GManager implements Runnable {

	public GWindow window = null;
	private Graphics2D g2d = null;
	private boolean bRunning = true;
	public GEvents events = null;
	public GLevel level = null;
	public GSprite sprite = null;
	
	public GManager(){
		window = new GWindow();
		events = new GEvents(window);
		level = new GLevel("res\\Headquarters.gif");
		sprite = new GSprite("res\\char.png");
	}
	public void run() {
		while (bRunning) {
			handleEvents();
			updateElements();
			//testPosition(); //Nao vou precisar movimentar o mapa enquanto move o sprite
			drawElements();
			sleep();
		}
		exitGame();
	}
	private void handleEvents()	{
		if (events.isKeyPressed(KeyEvent.VK_ESCAPE)) {
			bRunning = false;
		}
	}
	
	//Nao vou precisar movimentar o mapa enquanto move o sprite
	private void updateElements() {
		//sprite.update();
		//scrollLevel();
	}
	
	private void exitGame()	{
		System.exit(1);
	}
	
	private void sleep() {
		try {
			Thread.sleep(20);
		}
		catch(Exception e) {
			System.out.println(e);
			System.exit(1);
		}
	}
	
	private void drawElements()	{
		//Limpa a tela
//		g2d.setColor(Color.BLUE);
//		g2d.fillRect(0,0,window.getWidth(), window.getHeight());
		
		//Desenha o level/fase
		level.draw(g2d);
		
		//Desenha o sprite
		sprite.draw(g2d);
		
		//Repinta a janela
		window.repaintWindow();
	}
	
	private void init()	{
		window.init();
		g2d = window.getBackBuffer().createGraphics();
	}
	public void initLoop() {
		init();
		Thread thread = new Thread(this);
		thread.start();
	}

}
